import { Component, OnInit } from '@angular/core';
import { CommunicateService } from 'src/app/communicate.service';
import { ArticleService } from 'src/app/service/service/article.service';
import { Router } from '@angular/router';

interface article{
  article:string;
  artist:string;
  date:string;
  id:number;
  liked:number;
  title:string;
  viewed:number;
}

@Component({
  selector: 'app-article',
  templateUrl: './article.component.html',
  styleUrls: ['./article.component.less']
})
export class ArticleComponent implements OnInit {

  public locat=1
  public address:string='你好'
  public articlelife:Array<article>=[]
  public articlemark:Array<article>=[]
  public articlefront:Array<article>=[]
  public articleback:Array<article>=[]
  public class:Array<string>=['life','mark','front','back']
  constructor(
    private articleserve:ArticleService,
    private cs:CommunicateService,
    private router:Router
  ) { }

  ngOnInit(): void {
    this.getAll()
    document.addEventListener('scroll',()=>{this.next()
    })
  }
getArticleByClass(c:string){
  this.articleserve.getArticleByClass(c,1).toPromise()
  .then((data:any)=>{
    switch (c){
      case 'life':
        this.articlelife = data
        break;
      case 'mark':
        this.articlemark = data
        break;
      case 'front':
        this.articlefront = data
        break;
      case 'back':
        this.articleback = data
        break;
    }
  })
  .catch((err:any)=>console.log(err)
  )
}
 getAll(){
   this.getArticleByClass('life')
   this.getArticleByClass('mark')
   this.getArticleByClass('front')
   this.getArticleByClass('back')
}
next(){
  const x:number=document.documentElement.scrollTop
  switch (true){
    case (x>=0 && x<435):
      this.locat=1
      break;
    case (x>=435 && x<900):
      this.locat=2
      break;
    case (x>=900 && x<1335):
      this.locat=3
      break;
    case (x>=1335):
      this.locat=4
      break;
  }
}
// 侧边栏移动
setOffset(num:number){
  document.documentElement.scrollTop = num;
}
//跳转到具体类页
goto(address:string){
  this.cs.c = address
  // this.cs.emit(this.address)
  this.router.navigate(['/home/class'])
}
}
